﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Kingdee.BOS.Core.CloudModel.Cloud;
using Kingdee.BOS.Core.CloudModel;
using System.Data;
using Common;

namespace MethodSetBus
{
    public class SAL_RETURNSTOCKBus
    {
        public string Get_ReturnStockData(SaleOrder_OutStock _OutStock,DataTable dtBill)
        {
            Root<SAL_RETURNSTOCK> root = new Root<SAL_RETURNSTOCK>();
            root.IsDeleteEntry = "true";
            root.IsVerifyBaseDataField = "false";
            root.IsEntryBatchFill = "true";
            root.ValidateFlag = "true";
            root.NumberSearch = "true";
            root.IsAutoAdjustField = "false";
            root.IsControlPrecision = "false";
            root.ValidateRepeatJson = "false";
            try
            {

                SAL_RETURNSTOCK _RETURNSTOCK = new SAL_RETURNSTOCK();
				_RETURNSTOCK.FID = 0;
				_RETURNSTOCK.FBillTypeID = new OrgBase
				{
					FNumber = "XSTHD01_SYS"
				};
				_RETURNSTOCK.FDate = _OutStock.FDate;
				_RETURNSTOCK.FSaleOrgId = new OrgBase
				{
					FNumber = dtBill.Rows[0]["FNUMBER"].ToString()//_OutStock.FOrgNo//100
				};
				_RETURNSTOCK.FRetcustId = new OrgBase
				{
					FNumber = "CUST0301"
				};
				_RETURNSTOCK.FTransferBizType = new OrgBase
				{
					FNumber = "OverOrgSal"
				};
				_RETURNSTOCK.FStockOrgId = new OrgBase
				{
					FNumber = dtBill.Rows[0]["FNUMBER"].ToString()//_OutStock.FOrgNo//"100"
				};
				_RETURNSTOCK.FReceiveCustId = new OrgBase
				{
					FNumber = "CUST0301"
				};
				_RETURNSTOCK.FSettleCustId = new OrgBase
				{
					FNumber = "CUST0301"
				};
				_RETURNSTOCK.FPayCustId = new OrgBase
				{
					FNumber = "CUST0301"
				};
				_RETURNSTOCK.FOwnerTypeIdHead = "BD_OwnerOrg";
				_RETURNSTOCK.FIsTotalServiceOrCost = "false";
				_RETURNSTOCK.FReceiveAddress = _OutStock.FAddress;
				_RETURNSTOCK.FLinkMan = _OutStock.FCustomerName;
				_RETURNSTOCK.FLinkPhone = _OutStock.FPhoneNumber;

				//财务信息
				_RETURNSTOCK.SubHeadEntity = new SAL_RETURNSTOCK_HeadEntity();

				SAL_RETURNSTOCK_HeadEntity _RETURNSTOCK_HeadEntity = new SAL_RETURNSTOCK_HeadEntity();
				_RETURNSTOCK_HeadEntity.FSettleCurrId = new OrgBase
				{
					FNumber = "PRE001"
				};
				_RETURNSTOCK_HeadEntity.FSettleOrgId = new OrgBase
				{
					FNumber = dtBill.Rows[0]["FNUMBER"].ToString()//_OutStock.FOrgNo//"100"
				};
				_RETURNSTOCK_HeadEntity.FLocalCurrId = new OrgBase
				{
					FNumber = "PRE001"
				};
				_RETURNSTOCK_HeadEntity.FExchangeTypeId = new OrgBase
				{
					FNumber = "HLTX01_SYS"
				};
				_RETURNSTOCK_HeadEntity.FExchangeRate = 1;

				_RETURNSTOCK.SubHeadEntity = _RETURNSTOCK_HeadEntity;

				//明细
				_RETURNSTOCK.FEntity = new List<SAL_RETURNSTOCKEntity>();
				int i = 0;
                foreach (SaleOrder_OutStockEntry item in _OutStock.Entity)
				{
					string sql = $"exec Get_OUTSTOCKSrcData '{_OutStock.FNumber}','{item.FMaterialNo}'";
					DataTable dt = DBHelper.ExecuteQuery(sql);
                    if (dt.Rows.Count > 0)
                    {
                        if (string.IsNullOrWhiteSpace(_RETURNSTOCK.F_PFKX_Text))
                        {
							_RETURNSTOCK.F_PFKX_Text = dt.Rows[0]["F_PFKX_TEXT"].ToString();
						}
						

						SAL_RETURNSTOCKEntity entity = new SAL_RETURNSTOCKEntity();

						entity.FRowType = "Standard";
						entity.FMaterialId = new OrgBase
						{
							FNumber = item.FMaterialNo//"AXP-XX-003"
						};
						entity.FUnitID = new OrgBase
						{
							FNumber = dt.Rows[0]["FUnitNo"].ToString()//"Pcs"
						};
						entity.FRealQty = item.FQty;

                        if (_OutStock.Entity[i].FPrice == 0)
                        {
							entity.FIsFree = "true";
						}
                        else
                        {
							entity.FIsFree = "false";
						}

						//税率
						entity.FEntryTaxRate = Convert.ToDecimal(dt.Rows[0]["FTAXRATE"].ToString());

						entity.FReturnType = new OrgBase
						{
							FNumber = "THLX01_SYS"
						};
						entity.FOwnerTypeId = "BD_OwnerOrg";
						entity.FOwnerId = new OrgBase
						{
							FNumber = dtBill.Rows[0]["FNUMBER"].ToString()//_OutStock.FOrgNo//"100"
						};
						entity.FStockId = new OrgBase
						{
							FNumber = dt.Rows[0]["FStockNo"].ToString()//item.FStockNo//"CK001"
						};
						entity.FStockstatusId = new OrgBase
						{
							FNumber = "KCZT01_SYS"
						};
						entity.FDeliveryDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//"2023-07-04 00:00:00";
						entity.FSalUnitID = new OrgBase
						{
							FNumber = dt.Rows[0]["FUnitNo"].ToString()//"Pcs"
						};
						entity.FSalUnitQty = item.FQty;
						entity.FSalBaseQty = item.FQty;
						entity.FPriceBaseQty = item.FQty;
						entity.FIsOverLegalOrg = "false";
						entity.FARNOTJOINQTY = item.FQty;
						entity.FTaxPrice = Convert.ToDecimal(dt.Rows[0]["FTaxPrice"].ToString());//item.FPrice;
						entity.FDiscountRate = Convert.ToDecimal(dt.Rows[0]["FDiscountRate"].ToString());

						//entity.FAllAmount = Convert.ToDecimal(dt.Rows[0]["FTaxPrice"].ToString()) * item.FQty;

						//关联关系
						entity.FOrderNo = dt.Rows[0]["FSrcBillNo"].ToString();
		                //entity.FSOBILLTYPEID = dt.Rows[0]["FSrcType"].ToString();
						entity.FSOEntryId = Convert.ToInt32(dt.Rows[0]["FSOEntryId"].ToString());
						entity.FSrcBillTypeID = dt.Rows[0]["FOBJECTTYPEID"].ToString();
						entity.FSrcBillNo = dt.Rows[0]["FBILLNO"].ToString();
						
						entity.FIsReturnCheck = "false";
						entity.FSettleBySon = "false";
						entity.FMaterialID_Sal = new OrgBase
						{
							FNumber = item.FMaterialNo//"AXP-XX-003"
						};
						entity.FVmiBusinessStatus = "false";

						//明细关联关系
						entity.FEntity_Link = new List<SAL_RETURNSTOCK_Entity_Link>();
						SAL_RETURNSTOCK_Entity_Link _Entity_Link = new SAL_RETURNSTOCK_Entity_Link();
						_Entity_Link.FEntity_Link_FRuleId = "OutStock-SalReturnStock";
						_Entity_Link.FEntity_Link_FSTableName = "T_SAL_OUTSTOCKENTRY";
						_Entity_Link.FEntity_Link_FSBillId = Convert.ToInt32(dt.Rows[0]["FID"].ToString());//100069;
						_Entity_Link.FEntity_Link_FSId = Convert.ToInt32(dt.Rows[0]["FENTRYID"].ToString()); //100124;
						_Entity_Link.FEntity_Link_FBaseunitQtyOld = item.FQty;
						_Entity_Link.FEntity_Link_FBaseunitQty = item.FQty;
						_Entity_Link.FEntity_Link_FSalBaseQtyOld = item.FQty;
						_Entity_Link.FEntity_Link_FSalBaseQty = item.FQty;
						_Entity_Link.FEntity_Link_FAuxUnitQtyOld = item.FQty;
						_Entity_Link.FEntity_Link_FAuxUnitQty = item.FQty;
						entity.FEntity_Link.Add(_Entity_Link);

						_RETURNSTOCK.FEntity.Add(entity);
					}
					i++;

				}

				root.Model = _RETURNSTOCK;

				return JsonConvert.SerializeObject(root);
			}
            catch (Exception)
            {
				return JsonConvert.SerializeObject(root);
			}
        }


		/// <summary>
		/// 非空校验
		/// </summary>
		/// <param name="TypeData"></param>
		/// <returns></returns>
		public IsSuccessResult IsNullClounm(DataType<SaleOrder_OutStock> TypeData)
		{
			IsSuccessResult isSuccess = new IsSuccessResult();
			if (string.IsNullOrEmpty(TypeData.Data.FNumber))//订单编号
			{
				isSuccess.Code = 100;
				isSuccess.Result = null;
				isSuccess.Message = "单据编码不可为空！";
				return isSuccess;
			}
			if (string.IsNullOrEmpty(TypeData.Data.FDate))//下单日期
			{
				isSuccess.Code = 100;
				isSuccess.Result = null;
				isSuccess.Message = "下单日期不可为空！";
				return isSuccess;
			}
			for (int i = 0; i < TypeData.Data.Entity.Count; i++)//明细
			{
				if (string.IsNullOrEmpty(TypeData.Data.Entity[i].FMaterialNo))//物料编码
				{
					isSuccess.Code = 100;
					isSuccess.Result = null;
					isSuccess.Message = "商品编码不可为空！";
					return isSuccess;
				}
				if (TypeData.Data.Entity[i].FPrice == null || string.IsNullOrEmpty(TypeData.Data.Entity[i].FPrice.ToString()))//单价
				{
					isSuccess.Code = 100;
					isSuccess.Result = null;
					isSuccess.Message = "单价不可为空！";
					return isSuccess;
				}
				/*if (TypeData.Data.Entity[i].FPrice == null || TypeData.Data.Entity[i].FPrice == 0)//单价
				{
					isSuccess.Code = 100;
					isSuccess.Result = null;
					isSuccess.Message = "单价不可为空！";
					return isSuccess;
				}*/
				if (TypeData.Data.Entity[i].FQty == null || TypeData.Data.Entity[i].FQty == 0)//数量
				{
					isSuccess.Code = 100;
					isSuccess.Result = null;
					isSuccess.Message = "数量不可为空！";
					return isSuccess;
				}
				/*if (TypeData.Data.Entity[i].FSubtotal == null || TypeData.Data.Entity[i].FSubtotal == 0)//小计
				{
					isSuccess.Code = 100;
					isSuccess.Result = null;
					isSuccess.Message = "小计不可为空！";
					return isSuccess;
				}*/
			}

			isSuccess.Code = 0;
			isSuccess.Result = null;
			isSuccess.Message = null;
			return isSuccess;
		}


	}
}
